lucky.define(['view','element','perfectScrollbar','menu','component'],function(exports){
    var view = lucky.view,
        setter = lucky.setter,
        component = lucky.component,
        MOD_NAME = "admin",
        $body = $('body'),
        ELEMENT = {
            SCROLLABLE_CONTAINER:".scrollable-container",
            SCREENFULL_LAYOUT:".layout-screenfull",
            NAV_ELEM: "#main-menu-navigation"
        },
        elementHasExist = function (element) {
            return $(element).length > 0 ? true:false;
        };

    var admin = {
        initMenuElement: function(){
            $.app.menu.init(true);

            if($.app.nav.initialized === false){
                $.app.nav.init({speed: 300});
            }
        }

        , initMenuEvent: function(){
            $body.find("a.menu-router").on("click", function(event){
                event.preventDefault();
                var href = $(this).attr("href");

                location.hash = admin.correctRouter(href);
            });
        }

        , initLayout: function(){
            //初始化菜单事件
            admin.initMenuEvent();

            //全屏事件
            if(elementHasExist(ELEMENT.SCREENFULL_LAYOUT)){
                var elementScreenFull = $(ELEMENT.SCREENFULL_LAYOUT),
                    isSupportScreenFull = typeof screenfull != 'undefined',
                    ICON_MIN_CLASS = "ft-minimize ft-maximize",
                    ICON_MAX_CLASS = "ft-maximize ft-minimize";
                elementScreenFull.on("click",function(e){
                    if (isSupportScreenFull) {
                        if (screenfull.enabled) {
                            screenfull.toggle();
                        }
                    }
                });
                if (isSupportScreenFull) {
                    if (screenfull.enabled) {
                        $(document).on(screenfull.raw.fullscreenchange, function() {
                            if (screenfull.isFullscreen) {
                                elementScreenFull.find('i').toggleClass(ICON_MIN_CLASS);
                            } else {
                                elementScreenFull.find('i').toggleClass(ICON_MAX_CLASS);
                            }
                        });
                    }
                }
            }

            //通知&消息滚动条
            if(elementHasExist(ELEMENT.SCROLLABLE_CONTAINER) && $.fn.perfectScrollbar){
                $(ELEMENT.SCROLLABLE_CONTAINER).perfectScrollbar({
                    theme:"dark"
                });
            }
        }

        , initMenuActive: function(){
            var href = location.hash,
                navElement = $(ELEMENT.NAV_ELEM);
            if($.trim(href).length === 0){
                href = '#' + navElement.find("a.menu-router").eq(0).attr("href");
            }
            href = href.split("#")[1];

            navElement.find("a").parent("li").removeClass("active open");
            navElement.find("a[href='"+ href +"']").parent("li").addClass("active").parents("li").addClass("open");
        }

        , initAfterRender: function(){
            //初始化页面元素组建
            component.render();
        }

        //纠正路由格式
        , correctRouter: function(href){
            if (!/^\//.test(href)) href = '/' + href;

            //纠正首尾
            return href.replace(/^(\/+)/, '/');
        }

        //事件监听
        , on: function(events, callback){
            return lucky.onevent.call(this, setter.MOD_NAME, events, callback);
        }
    };

    //事件
    var events = admin.events = {
        logout: function (othis) {
            view.logout();
        }
    };

    $body.on("click", "*[admin-event]", function () {
        var othis = $(this),
            attrEvent = othis.attr("admin-event");
        if($.trim(attrEvent).length <= 0){
            return;
        }
        events[attrEvent] && events[attrEvent].call(this, othis);
    });

    Unison.on('change', function(){
        $.app.menu.change();
    });

    //左侧菜单控制显示/缩放/隐藏事件
    $(document).on('click', '.menu-toggle, .modern-nav-toggle', function(e){
        e.preventDefault();

        var collapsedSidebar = $('#collapsed-sidebar');
        $.app.menu.toggle();

        setTimeout(function() {
            $(window).trigger("resize");
        }, 200);

        if (collapsedSidebar.length > 0) {
            setTimeout(function() {
                if ($body.hasClass('menu-expanded') || $body.hasClass('menu-open')) {
                    collapsedSidebar.prop('checked', false);
                } else {
                    collapsedSidebar.prop('checked', true);
                }
            }, 1000);
        }
        return false;
    });
    //关闭菜单 mobile使用
    $(document).on('click', '.close-navbar', function(e){
        e.preventDefault();
        $.app.menu.toggle();
    });

    $(document).on('click', '.open-navbar-container', function(e) {
        var currentBreakpoint = Unison.fetch.now();
        $.app.menu.drillDownMenu(currentBreakpoint.name);
    });

    $(window).resize(function() {
        $.app.menu.manualScroller.updateHeight();
    });

    exports(MOD_NAME,admin);
});